home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12394 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  49 lines

  1. Path: longwood.cs.ucf.edu!not-for-mail
  2. From: schnitzi@longwood.cs.ucf.edu (Mark Schnitzius)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Is This Bad Coding Practice?
  5. Date: 30 Mar 1996 16:01:02 -0500
  6. Organization: University of Central Florida
  7. Message-ID: <4jk7ee$mtf@longwood.cs.ucf.edu>
  8. References: <4jgnt2$9d1@loki.tor.hookup.net>
  9. NNTP-Posting-Host: longwood.cs.ucf.edu
  10.  
  11. Rajendra_Singh@msn.com (Rajendra Singh) writes:
  12.  
  13. >int main(int argc, char *argv[])
  14. >   {
  15. >   char string[128];
  16. >   char *func1(void);
  17.  
  18. >   strcpy(string, func1());
  19. >   return 0;
  20. >   }
  21.  
  22. >char *func1(void)
  23. >   {
  24. >   char test[100];
  25.  
  26. >   sprintf(test, "Test:  %d", 1);
  27. >   return test;
  28. >   }
  29.  
  30. >... since I am using the value returned from func1() immediately (in
  31. >main()), is this reliable?  After I copy it into "string", I won't be
  32. >using that area of memory anymore (i. e. the pointer returned by
  33. >func1()).
  34.  
  35.  
  36. Yes, this is bad coding practice.  There's no guarantee that the 
  37. strcpy function itself won't try to use some of the freed up memory
  38. allocated in func1.
  39.  
  40. Why not just pass a pointer to 'string' into the function?
  41.  
  42.  
  43.  
  44. _____________________________________________________________
  45. mark schnitzius - - - - - - - - - - - - - schnitzi@mentos.com
  46.        <a href="http://east.isx.com/~schnitzi/">me</a>
  47. "I don't know if it's good that they did invent the wheel
  48.  But ever since... I've been rolling on to you" --Frank Black
  49.